package com.bub.pay.core.manager.system.async.impl;

import com.alibaba.fastjson.JSON;
import com.bub.pay.core.entity.bo.system.SysLogAsyncRecordBO;
import com.bub.pay.core.manager.system.async.SysLogAsyncManager;
import com.bub.pay.framework.common.async.ExceptionHandlingAsyncTaskExecutor;
import com.bub.pay.core.entity.domain.SysLog;
import com.bub.pay.core.manager.system.SysLogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SysLogAsyncManagerImpl implements SysLogAsyncManager {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ExceptionHandlingAsyncTaskExecutor taskExecutor;

    @Resource
    private SysLogManager sysLogManager;

    @Override
    public void addSysLog(SysLogAsyncRecordBO sysLogAsyncRecordBO) {
        // 异步保存日志
        taskExecutor.execute(() -> {
            logger.debug("addSysLog ==> {}", JSON.toJSONString(sysLogAsyncRecordBO));
            try {
                SysLog sysLog = new SysLog();
                BeanUtils.copyProperties(sysLogAsyncRecordBO, sysLog);
                sysLogManager.insertSelective(sysLog);
            } catch (Exception e) {
                // 此时出现异常 降级为打印日志
                logger.warn("addSysLogError,sysLogAsyncRecordBO:{}", JSON.toJSONString(sysLogAsyncRecordBO));
                logger.error("addSysLogError", e);
            }
        });
    }

}
